package schedules;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import java.util.Date;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SchedulerMetaData;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CronTriggerEjecCero {
	
	  public void run() throws Exception {
        Logger log = LoggerFactory.getLogger(CronTriggerEjecCero.class);

	        log.info("------- Initializing -------------------");

	        // First we must get a reference to a scheduler
	        SchedulerFactory sf = new StdSchedulerFactory();
	        Scheduler sched = sf.getScheduler();

	        log.info("------- Initialization Complete --------");

	        log.info("------- Scheduling Jobs ----------------");

	        // jobs can be scheduled before sched.start() has been called

	        // job 1 will run every 20 seconds
	        JobDetail job = newJob(SimpleJob.class)
	            .withIdentity("job1", "group1")
	            .build();
	        
	        CronTrigger trigger = newTrigger()
	            .withIdentity("trigger1", "group1")
	            .withSchedule(cronSchedule("0 28 19 * * ?"))
	            .build();

	        Date ft = sched.scheduleJob(job, trigger);
	        log.info(job.getKey() + " has been scheduled to run at: " + ft
	                + " and repeat based on expression: "
	                + trigger.getCronExpression());
	        
	        //---------------------------------------------------------------
	        job = newJob(CalendarioJob.class)
		            .withIdentity("job2", "group1")
		            .build();
		        
		    trigger = newTrigger()
		            .withIdentity("trigger2", "group1")
		            .withSchedule(cronSchedule("0 27 19 * * ?"))
		            .build();

		    ft = sched.scheduleJob(job, trigger);
		        log.info(job.getKey() + " has been scheduled to run at: " + ft
		                + " and repeat based on expression: "
		                + trigger.getCronExpression());
	        
	  
	       //log.info("------- Started Scheduler -----------------");

          //log.info("------- Waiting five minutes... ------------");    
	      try {
        
	    	
	    	// sched.scheduleJob(job,trigger); 
	    	 sched.start();
          } catch (Exception e) {
      }

      //log.info("------- Shutting Down ---------------------");

      //sched.shutdown(true);

      //log.info("------- Shutdown Complete -----------------");

      //SchedulerMetaData metaData = sched.getMetaData();
      //log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");

  }
	  
  public void stop(){
	  
		  
  }
	  	  

  public static void main(String[] args) throws Exception {

	  CronTriggerEjecCero example = new CronTriggerEjecCero();
      example.run();
  }

}
